package com.tefl.servlet.listner;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.tefl.cache.BasicCacheDictionary;
import com.tefl.common.util.ConvertHelper;
import com.tefl.common.util.DataBaseHelper;
import com.tefl.entity.Dept;
import com.tefl.entity.InfGrade;
import com.tefl.entity.InfStatus;
import com.tefl.entity.SysMenu;

/**
 * 初始化后台菜单缓存
 * 
 * @author liuxin
 * 
 */
public class BasicDataCacheListner implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent event) {

	}

	/**
	 * 初始化   完成缓存加载
	 */
	@Override
	public void contextInitialized(ServletContextEvent event) {
		try {
			List<Dept> deptList = DataBaseHelper.getAllDept();
			List<InfStatus> statusList = DataBaseHelper.getAllStatus();
			List<InfGrade> gradeList = DataBaseHelper.getAllGrade();
			List<SysMenu> menuList = DataBaseHelper.getAllMenu();

			BasicCacheDictionary.DEPT_CACHE_MAP = ConvertHelper
					.transferListToMap(deptList, "dept_id");
			BasicCacheDictionary.GRADE_CACHE_MAP = ConvertHelper
					.transferListToMap(gradeList, "inf_grade_id");
			BasicCacheDictionary.STATUS_CACHE_MAP = ConvertHelper
					.transferListToMap(statusList, "status_id");
			BasicCacheDictionary.SYS_MENU_CACHE_LIST = menuList;
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

}
